package com.common;

import com.common.utils.StringUtils;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import java.util.HashMap;
import java.util.Map;

public enum ErrorCodeEnum {
    /** 系统错误 */
    SUCCESS("0000", "操作成功"),
    ERROR_MESSAGE("0001", "错误描述:{}"),
    FAILED("9999", "未知错误"),
    USER_DETAILS_FAILED("0001", "用户信息获取失败"),
    USER_NAME_FAILED("0002", "用户名获取失败"),
    USER_UUID_NAME_FAILED("0003", "随机游客用户名创建失败"),
    ANONYMOUS_LOAD_FAILED("0004","游客信息读取失败"),
    ANONYMOUS_SAVE_FAILED("0005","游客信息保存失败"),
    UPDATE_USER_IMAGE_FAILED("0006","更新用户头像失败,请联系管理员"),
    MD5_FAILED("0007","MD5加密失败"),
    SYNC_TABLE_COLUMN_FAILED("0008","同步表字段失败，表不存在"),
    COPY_LIST_FAILED("0009","数组复制失败"),
    COPY_FAILED("0010","复制失败"),
    DATA_NOT_FOUNT("0011","数据不存在"),
    VERIFY_NULL_FAILED("0012","对象非NULL校验出错，name:{}"),
    UTIL_FAILED("0013", "工具类异常"),
    FILE_FAILED("0014", "文件类异常"),
    FINGERPRINT_FORMAT_FAILED("0015", "设备唯一ID不合法"),
    FINGERPRINT_NOT_FOUND("0016","设备ID不存在"),
    SERVICE_REQUEST_FAILED("0017","监控服务请求失败"),
    REDIS_CONNECT_FAILED("0018","Redis服务连接出错"),
    STRING_COMPARE_FAILED("0019","字符串相等，无大小之分"),
    DATA_ABNORMAL("0020","数据异常,数据库存在脏数据,{}"),

    /** 10**~10** 外部请求参数问题 */
    PARAM_FAILED("1001", "参数非法,{}"),
    UPLOAD_FILE("1002","上传文件失败"),
    JSON_PARSE_FAILED("1003", "JSON反序列化失败非法"),
    DATA_STATUS_FAILED("1004","数据类型错误(0:正常 1:停用 2:删除) {}"),
    USER_TYPE_FAILED("1005","用户类型错误(0:用户,1:管理员,2:游客) {}"),
    DATA_SCOPE("1006","数据权限参数错误(0:全部数据权限 1:自定数据权限 2:部门数据权限 3:部门及以下数据权限 4:仅本人数据权限) {}"),
    TOKEN_FAILED("1020","token解析失败"),
    TOKEN_CREATE_FAILED("1021","token创建失败"),

    /** 11**~115* 用户校验相关 */
    USER_VERIFY("1100","校验失败:{}"),
    USER_DELETE("1101","用户已删除,id:{}"),
    USER_STOP("1102","用户已停用,id:{}"),
    USER_FORBID("1103","该帐号已停用"),
    USER_PASSWORD_FAILED("1104","密码错误"),
    USER_CHECK_FAILED("1105","用户校验失败"),
    USER_NAME_EXISTS("1106","用户登录名已存在"),
    USER_MOBILE_EXISTS("1107","手机号已存在"),
    USER_EMAIL_EXISTS("1108","邮箱已存在"),
    USER_DATA_NOT_FOUND("1109","用户不存在,{}"),
    USER_OLD_PASSWORD_FAILED("1110","原密码错误"),
    BAN_ANONYMOUS_LOGIN("1111","禁止游客使用账号密码登录"),
    USER_FAILED("1112","账号或密码错误"),
    LOGIN_TOO_MUCH("1113","登录次数过多,请{}分钟后继续尝试"),
    RESET_PASSWORD_TIMEOUT("1114","重置密码时间已超时"),
    ROOT_USER("1115","无法删除管理员"),

    /** 116*~119* 校验相关 */
    MOBILE_CODE_VERIFY_FAILED("1160","短信验证码验证失败"),
    MOBILE_CODE_SEND_FAILED("1161","短信验证码发送失败"),
    UNLOCK_CODE_VERIFY_FAILED("1170","滑块验证失败"),
    UNLOCK_PASSED_CODE_VERIFY_FAILED("1171","滑块密钥失效"),
    UNLOCK_STOP("1172","滑块验证未启动"),
    EMAIL_CODE_VERIFY_FAILED("1180","邮箱验证码验证失败"),
    EMAIL_CODE_SEND_FAILED("1181","邮箱验证码发送失败"),

    /** 12**~124* 接口权限相关 */
    UNAUTHORIZED("1200","身份认证失败，请重新登录"),
    NOT_FOUNT_PERMISSION("1201","该保护对象不允许任何人访问"),
    NOT_FOUNT_USER_PERMISSION("1202","该用户无任何权限"),
    INADEQUATE_PERMISSION("1203","该用户权限不足"),

    /** 125*~129* 角色相关 */
    ROLE_NOT_FOUNT("1250","角色不存在"),
    PERMISSIONS_NAME_FAILED("1251", "权限字段不存在"),
    ROOT_ROLE("1252", "根角色不允许修改"),

    /** 13*~1399 部门相关 */
    PARENT_DEPT_NOT_FOUND("1301", "父级部门已停用或不存在"),
    DEPT_NOT_FOUND("1302", "部门不存在"),
    DEPT_NAME_EXISTS("1303","部门名字已存在"),
    DEPT_STOP("1304", "部门已停用,id:{},name:{}"),
    DEPT_DELETE("1304", "部门已删除,id:{},name:{}"),
    DEPT_IDS_NOT_FOUND("1310", "自定义数据权限部门列表不能为空"),
    DEPT_USER("1311","仍有用户与该部门关联，无法删除"),
    FATHER_DEPT("1312","根部门无法删除"),
    DEPT_CHILD("1313","仍有子部门与该部门关联，无法删除"),

    /** 14*~1499 菜单相关 */
    MENU_STOP("1400","菜单已停用,id:{},name:{}"),
    MENU_DELETE("1401","菜单已删除,id:{},name:{}"),
    MENU_NOT_FOUND("1402","菜单不存在"),

    /** 15**~1599* 上传相关 */
    EXCEL_NOT_FOUNT("1500", "导入内容为空"),

    /** 17**~1799  高速上链相关 */
    ROOT_HASH_NO_EQUALS("1700","MerkleTree根节点不一致，数据不一致"),
    FINGERPRINT_NO_EQUALS("1701","指纹不一致"),
    UPLOAD_CHAIN_FAILED("1702","数据未完成上链，无法校验"),

    /** 18**~1899 MTQQ服务相关*/
    MTQQ_CLOSE("1800","MTQQ服务未启动"),
    ;


    ErrorCodeEnum(String code, String desc) {
        errorCode = code;
        errorMsg = desc;
    }

    private final String errorCode;
    private  String errorMsg;
    private static final ThreadLocal<Map<String,String>> errorParamMsg = new ThreadLocal<>();

    private static Map<String,String> getThreadMsg(){
        Map<String,String> map=errorParamMsg.get();
        if(StringUtils.isNull(map)) {
            map=new HashMap<>();
            errorParamMsg.set(map);
        }
        return  map;
    }

    public ErrorCodeEnum setParam(String... appendLogArguments){
        FormattingTuple ft = MessageFormatter.arrayFormat(this.errorMsg, appendLogArguments);
        Map<String,String> map=getThreadMsg();
        map.put(this.errorCode,ft.getMessage());
        return this;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMsg() {
        Map<String,String> map=getThreadMsg();
        String threadErrMsg=map.get(this.errorCode);
        if(StringUtils.isNotEmpty(threadErrMsg)){
            return threadErrMsg;
        }
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
